package com.ebook.ui.activity;

import com.ebook.ui.fragment.bookfind.Keyword;
import com.ebook.ui.fragment.home.category.CategoryFragment;
import com.ebook.ui.fragment.home.everyrecommend.EveryEliteChooseFragment;
import com.ebook.ui.fragment.home.littlebookcase.LittleBookCase;
import com.ebook.ui.fragment.home.shuffle.ShufflingFragment;
import com.ebook.ui.fragment.home.usercenter.UserCenter;
import com.ebook.ui.logic.BookDistributer;
import com.ebook.ui.sync.WorkSyncer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class HomeActivity extends Activity{
    private CategoryFragment categoryFragment;
    private ShufflingFragment shufflingFragment;
    private EveryEliteChooseFragment everyEliteChooseFragment;
    private UserCenter userCenter;
    private LittleBookCase littleBookCase;
    public HomeActivity() throws NoSuchFieldException, IllegalAccessException {
        super();
        initialize();
        loadActivity();
    }

    @Override
    public void initialize() {
        this.setSize(1200,800);
        JLabel logo = new JLabel("电子书阅读");
        this.add(logo);
        logo.setBounds(0,0,200,60);
        logo.setFont(new Font("微软雅黑",Font.PLAIN,18));
        logo.setForeground(Color.RED);
        JTextField searchBox = new JTextField();
        searchBox.setBounds(364,20,550,40);
        searchBox.addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e){
                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    try {
                        WorkSyncer.webView.pushActivity(new BookFindActivity(
                                BookDistributer.distributer(new Keyword(searchBox.getText()))
                        ));
                    } catch (NoSuchFieldException | IllegalAccessException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        this.add(searchBox);
        JButton searchGo = new JButton("search");
        searchGo.setBounds(914,20,100,40);
        this.add(searchGo);
        searchGo.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e) {
                searchGo.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            @Override
            public void mouseClicked(MouseEvent e){
                try {
                    WorkSyncer.webView.pushActivity(new BookFindActivity(
                            BookDistributer.distributer(new Keyword(searchBox.getText()))
                    ));
                } catch (NoSuchFieldException | IllegalAccessException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    @Override
    public void loadActivity() throws NoSuchFieldException, IllegalAccessException {
        super.loadActivity();
        ArrayList<Class<?>> clazz = new ArrayList<>();
        clazz.add(CategoryFragment.class);
        clazz.add(EveryEliteChooseFragment.class);
        clazz.add(ShufflingFragment.class);
        clazz.add(UserCenter.class);
        clazz.add(LittleBookCase.class);
        super.loadFragmentFromClasses(clazz);
        categoryFragment = (CategoryFragment) getFragmentByClass(CategoryFragment.class);
        this.add(categoryFragment);
        shufflingFragment = (ShufflingFragment) getFragmentByClass(ShufflingFragment.class);
        this.add(shufflingFragment);
        userCenter = (UserCenter) getFragmentByClass(UserCenter.class);
        this.add(userCenter);
        littleBookCase = (LittleBookCase)getFragmentByClass(LittleBookCase.class);
        this.add(littleBookCase);
    }

    @Override
    public void exitActivity() {
        super.exitActivity();
    }

    @Override
    public void destroyActivity() {
        super.destroyActivity();
    }
}
